package com.ten.mall.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ten.mall.service.ItemService;
import com.ten.mall.service.YaodianService;
import com.ten.mall.vo.GoodsYaodian;
import com.ten.mall.vo.Item;
import com.ten.mall.vo.KucunResult;
import com.ten.mall.vo.Yaodian;

@Controller
public class ItemController {
  @Autowired
  private ItemService itemService;
  
  @Autowired
  private YaodianService yaodianService;
  
  /**
   * 后台产品
   * @param start
   * @param size
   * @param model
   * @param name
   * @return
   */
  @RequestMapping("/admin/itemList")
  public String itemList(@RequestParam(name="start",defaultValue="1") Integer start,
		                 @RequestParam(name="size",defaultValue="10") Integer size,
			             Model model,String name){
	  
	  Map<String,Object> map =itemService.selectByPages(start, size, name);
	  List<Item> list =(List<Item>) map.get("list");
	  model.addAttribute("list", list);
	  Long mySize=(Long) map.get("total");
	  int total =mySize.intValue()/10+1;
	  model.addAttribute("page", start);
	  model.addAttribute("total", total);
	  return "jsp/itemList";
  }
  /**
   * 增加产品
   * @param id
   * @param model
   * @return
   */
  @RequestMapping("/admin/toAddItem")
  public String toAddItem(Integer id,Model model){
	  if(id!=null){
		  Item item =itemService.selectById(id);
		  model.addAttribute("item", item);
	  }
	  return "jsp/addItem";
  }
  @RequestMapping("/admin/saveItem")
  @ResponseBody
  public String saveItem(Item item){
	  String rs="error";
	  try {
		if(item.getId()!=null){
			modifyItem(item);
			itemService.updateItem(item);
		}else{
			modifyItem(item);
			itemService.insertItem(item);
			}
		rs="ok";
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	  return rs;
  }
  @RequestMapping("/admin/itemDel")
  @ResponseBody
	public String itemDel(Integer id){
		String ss="ok";
		try {
			if(id!=null){
				itemService.deleteItem(id);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			ss="error";
		}
		return ss;
	}
  /**
   * 药店库存管理
   * @param start
   * @param size
   * @param record
   * @return
   */
  @RequestMapping("/admin/yaodianKucunList")
  public String yaodianKucunList(@RequestParam(name="start",defaultValue="1") Integer start,
          						 @RequestParam(name="size",defaultValue="10") Integer size,
          						 KucunResult record,HttpServletRequest res){
	  List<KucunResult> list =itemService.selectYaodianKucun(start, size, record);
	  int total=list.size()/10+1;
	  res.setAttribute("page", start);
	  res.setAttribute("total", total);
	  res.setAttribute("list", list);
	  return "jsp/yaodianKucunList";
  }
  /**
   * 
   * @return
   */
  @RequestMapping("/admin/yaodianKucunAdd")
  public String yaodianKucunAdd(HttpServletRequest res,KucunResult vo){
	  KucunResult result =itemService.selectYaodianKucun(vo);
	  res.setAttribute("result", result);
	  List<Yaodian> list =yaodianService.selectAll(null);
	  res.setAttribute("list", list);
	  return "jsp/yaodianKucunAdd";
  }
  /**
   * 保存药店库存
   * @param vo
   * @return
   */
  @RequestMapping("/admin/yaodianKucunSave")
  public String yaodianKucunSave(GoodsYaodian vo){
	  /*vo.setRemark(null);
	  vo.setYaoDetail(null);*/
	  itemService.insertGoodsYaodian(vo);
	  return "redirect:/admin/yaodianKucunList";
  }
  /**
   * 修改图片属性
   * @param item
   * @return
   */
  private Item modifyItem(Item item){
	  if(StringUtils.isNotBlank(item.getGoodsRemark())){
			String remark =item.getGoodsRemark();
			if(remark.endsWith(",")){
				remark =remark.substring(0, remark.length()-1);
			}
			String [] str =remark.split(",");
			item.setGoodsPic(str[0]);
			item.setGoodsRemark(remark);
		}
	  return item;
  }
 
}
